﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using HFK.DxFluentUI.Utility;
using HFK.DxFluentUI.LanguageModel.Editors;

namespace HFK.DxFluentUI.Editors
{
  public class DropdownBoxContext<ComponentType> : DropdownBoxElement<ComponentType>, DropdownAssignValue<ComponentType>
  {
    public static DropdownBoxContext<ComponentType> Create()
    {
      return new DropdownBoxContext<ComponentType>();
    }

    #region DropdownBoxElement<ComponentType> Members

    public DropdownBoxElement<ComponentType> DisplayProperty<PropertyType>(Expression<Func<ComponentType, PropertyType>> expression)
    {
      ImplementingControl.DisplayMember = ChildPropertySyntax.Create<ComponentType, PropertyType>(expression).PropertyName;
      ImplementingControl.Columns.AddRange(new DevExpress.XtraEditors.Controls.LookUpColumnInfo[] {
            new DevExpress.XtraEditors.Controls.LookUpColumnInfo(ImplementingControl.DisplayMember, "DisplayProperty")});
      ImplementingControl.ShowHeader = false;

      return this;
    }

    public DropdownAssignValue<ComponentType> AssignValue()
    {
      return this;
    }

    #endregion

    #region DropdownAssignValue<ComponentType> Members

    public DropdownAssignValue<ComponentType> FromProperty<PropertyType>(Expression<Func<ComponentType, PropertyType>> expression)
    {
      ImplementingControl.ValueMember = ChildPropertySyntax.Create<ComponentType, PropertyType>(expression).PropertyName;

      return this;
    }

    public DropdownAssignValue<ComponentType> FromObject()
    {
      ImplementingControl.ValueMember = DevExpressPropertyDescriptorSyntax.ObjectReference();
      return this;
    }

    #endregion

    internal DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit ImplementingControl { get; private set; }

    private DropdownBoxContext()
    {
      ImplementingControl = new DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit();  
    }
  }
}
